﻿using famails.common;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.template
{
    public partial class Site : System.Web.UI.MasterPage
    {
        UserLoginDTO userLogin;
        public string tabClass = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            tabClass = Session["groupId"] + "";
            if (!IsPostBack)
            {
                try
                {
                    userLogin = getUserLogin();
                    checkRole();
                    LoadCurrentDate();
                    loadLimitWithUser();
                    LoadMainMenu();
                    if (userLogin != null)
                    {
                        lblInfo.Text = userLogin.Username;
                        lkClientDetail.NavigateUrl = "../page/clientdetail.aspx";
                        lkClientDetail.Visible = false;// userLogin.UserType == (int)(int)Common.DepartmentId.Client;
                        //if (userLogin.UserType == 0)
                        //    hplManager.NavigateUrl = "../../page/backend/clientregister.aspx";
                        //else
                        //    hplManager.NavigateUrl = "../../page/backend/customer.aspx";
                    }
                    else
                    {
                        //Response.Redirect("login.aspx");
                    }
                }
                catch (Exception ex)
                {
                    //throw ex;
                    //Response.Redirect("login.aspx");
                }

            }

        }
        void LoadMainMenu()
        {
            bus.RoleListBUS user = new bus.RoleListBUS();
            DataTable menu = user.GetMainMenuByDepartmentId(userLogin.UserType);
            listMainMenu.DataSource = menu;
            listMainMenu.DataBind();
        }

        private void loadLimitWithUser()
        {

            //if (getUserLogin().DepartmentId == 1)
            //{
            //    hplLimit.Text = "Gửi & Tạo KH: Không giới hạn";
            //}
            //else if (getLimitWithUser() != null)
            //{
            //    hplLimit.Text = "Gửi: " + getUserLogin().hasSendMail + "/" + getLimitWithUser().limitSendMail
            //        + ", Tạo KH: " + getUserLogin().hasCreatedCustomer + "/" + getLimitWithUser().limitCreateCustomer;
            //}
            //else
            //{
            //    hplLimit.Text = "Gửi & Tạo KH: Không giới hạn";
            //}

        }
        private void checkRole()
        {
            try
            {
                UserLoginDTO ulDto = getUserLogin();
                foreach (Control child in ControlPanel.Controls)
                {
                    if (child is HyperLink)
                    {
                        HyperLink hplRole = (HyperLink)child;
                        hplRole.Visible =
                              Common.checkRoleByRoleId(int.Parse(hplRole.Attributes["roleId"]), ulDto.UserType);
                    }
                }

            }
            catch (Exception ex)
            {
            }

        }
        public string GetCurrentPageName()
        {
            string urlPath = Request.Url.AbsolutePath;
            FileInfo fileInfo = new FileInfo(urlPath);
            string pageName = fileInfo.Name;
            return pageName;
        }
        protected void LoadCurrentDate()
        {
            DateTime d = DateTime.Now;
            string[] thu = { " Chủ nhật", " Thứ hai", " Thứ ba", " Thứ tư", "Thứ năm", " Thứ sáu", " Thứ bảy" };
            int i = d.DayOfWeek.GetHashCode();
            string currentDate = "Hôm nay: " + thu[i] + ", ngày " + d.ToString("dd/MM/yyyy") + ", hiện tại là: " + d.ToString("HH:mm");
            hplCurrentDate.Text = currentDate;
        }
        protected void lbtLogout_Click(object sender, EventArgs e)
        {
            Session["us-login"] = null;
            Response.Redirect("../page/login.aspx");
        }

        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            return null;
        }

        private RoleDetailDTO getLimitWithUser()
        {
            try
            {
                if (Session["limitWithUser"] != null)
                {
                    return (RoleDetailDTO)Session["limitWithUser"];
                }
            }
            catch (Exception)
            {
                return null;
            }
            return null;
        }

        protected void hplSendMail_Click(object sender, EventArgs e)
        {
            LinkButton link = (LinkButton)sender;
            Session["groupId"] = link.Attributes["roleId"];
            Session["linkId"] = link.Attributes["parentId"];
            Response.Redirect(link.CommandArgument);
        }
    }
}